package cn.dxc.demo.config;

import io.swagger.annotations.Api;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
@Api("权限管理")
public class ShiroFilterConfiguration {

    //final private String ip = "http://192.168.1.110:8080/";
    /**
     * 创建过滤工厂
     * */
    @Bean // 使用在方法上，标注将该方法的返回值存储到Spring容器中
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        /**
         * shiro内置过滤器，实现权限相关的拦截器
         * anon:无需登录可以访问
         * authc:需要登录才可以访问
         * */

        /*Map<String,String> filterMap = new LinkedHashMap<>();
        //filterMap.put("/admin/**","anon");
        //filterMap.put("/layui/**","anon");
        filterMap.put("ip+/login","anon");
        filterMap.put("/toLogin","anon");

        *//**
         * swagger2 过滤配置
         *//*
        filterMap.put("/webjars/**","anon");
        filterMap.put("/swagger-ui.html/**","anon");
        filterMap.put("/swagger-resources/**","anon");
        filterMap.put("/v2/**","anon");
        filterMap.put("/doc.html/**","anon");

        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        // 不登录自动跳转页面
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 登录后自动跳转的页面
        shiroFilterFactoryBean.setSuccessUrl("");*/
        return shiroFilterFactoryBean;
    }


    /*
     * 权限管理
     * */
    @Bean("securityManager")
    public DefaultWebSecurityManager defaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm());
        return defaultWebSecurityManager;
    }


    /**
     * 创建Realm ，登录用
     * */
    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        UserRealm userRealm = new UserRealm();
        return userRealm;
    }

    /**
     *
     * thymeleaf 整合 Shiro
     * */

    /*@Bean
    public ShiroDialect shiroDialect(){
        return  new ShiroDialect();
    }*/
}
